import util from "../../util";
import parent from '../../others/parent/parent';

export default class apiConfig extends parent{
    constructor(path) {
        super(apiConfig, util.api.ApiConfig);
    }
    static create(record, path){
        let object=new apiConfig(path);
        if(!!record){
            object.setRecord(record);
        };
        return object;
    }
    /***
     * 新增
     * @param record
     * @returns {Promise<boolean|*>}
     */
    async add(record=this.record){
        if(!record||!record.sysName||!record.serviceName||record.sysName==''||record.serviceName==''){
            return false;
        }
        let newRecord=Object.assign(this.clz.tpl(),record);
        let result = await this.api.add(newRecord).then(data=>{
            if(!!data){
                this.setRecord(data);
                return true;
            }else{
                return false;
            }
        }).catch(err=>{
            return false;
        });
        return result;
    }
    serviceItemTpl() {
        return apiConfig.serviceItemTpl()
    }
    /* 新增模板 */
    static tpl(){
        return {
            sysName: 'yongyou',
            serviceName: 'u8cloud/api/',
            config: [],
            version: "1.0",
            published: true,
            comments: "",
        }
    }
    static serviceItemTpl() {
        return {
            ref: {
                model: '',
                className: '',
                refProps: []
            },
            mapper: {},
            hierFlag: false,
            masterFlag: true,
            model: '',
            className: '',
            condition: '',
            sourceName: '',
            sourceType: null, // 1, 2, 3, 4
            parentSourceName: '',
            parentSourceType: null
        }
    }
}
